package main

import (
	"fmt"
	"html/template"
	"net/http"
)

type UserInfo struct {
	Name   string
	Gender string
	Age    int
}

func main() {
	http.HandleFunc("/", HelloHandleFunc)
	http.HandleFunc("/index", IndexHandleFunc)
	http.ListenAndServe(":8086", nil)
}

func IndexHandleFunc(writer http.ResponseWriter, request *http.Request) {
	t, err := template.ParseFiles("D:\\project-web-go\\project-hello\\project_learning\\chapter_02\\template\\04_template_index.html")
	if err != nil {
		fmt.Println("模板解析失败,err:", err)
		return
	}
	//2渲染模板
	user := UserInfo{
		Name:   "小许",
		Gender: "男",
		Age:    28,
	}
	t.Execute(writer, user)

}

func HelloHandleFunc(writer http.ResponseWriter, request *http.Request) {
	t, err := template.ParseFiles("D:\\project-web-go\\project-hello\\project_learning\\chapter_02\\template\\04_template_example.html")
	if err != nil {
		fmt.Println("模板解析失败,err:", err)
		return
	}
	//2渲染模板
	name := "我爱go语言"
	t.Execute(writer, name)

}
